<template>
  <div class="upload-box">
    <el-upload
      class="upload-demo"
      action="https://jsonplaceholder.typicode.com/posts/"
      :disabled="disabled"
      :http-request="(file)=>{return uploadFn(file,'1')}"
      :show-file-list="false">
      <el-button size="small" type="text" v-show="!disabled ">上传</el-button>
    </el-upload>
  </div>
</template>

<script>
  // import { upFile } from '/index.js'
  import request from '@/utils/request'
  export default {
    name: "FileUpload",
    fileUrl:'/file/upload',
    props: {
      disabled: {
        type: Boolean,
        default: () => (false)
      },
    },
    methods: {
      upFile(query) {
        return request({
          url: `/file/upload`,
          method: 'post',
          data: query
        })
      },

      downloadFn(query) {
        return request({
          url: `/file/download/${query}`,
          method: 'get',
        })
      },


      uploadFn(e){  // 文件上传
        const file = e.file;
        const form = new FormData();
        // 文件对象
        form.append("file", file);
        this.upFile(form).then( res => {
          console.log('文件上传:',res);
            if(res && res.code ===0){
              this.$emit('handleUploadSuccess',res.data);
            }else{
              this.msgError(res.msg);
            }
          }
        );
      },
    },
  }
</script>

<style scoped  lang="scss">

</style>
